﻿using System;
using System.Collections.Generic;
using Quantum.Core;
using RabbitMQ.Client;

namespace Quantum.RabbitMq
{
    public abstract class RabbitMqConnetion : IConnectionBehavior
    {
        public IConnection Connection { get; private set; }

        public bool Connect(string baseUrl, IDictionary<string, object> clientProperties = null, string username = "guest", string password = "guest")
        {
            try
            {
                var factory = new ConnectionFactory { HostName = baseUrl };
                factory.UserName = username;
                factory.Password = password;
                if (clientProperties != null)
                {
                    factory.ClientProperties = clientProperties;
                }

                Connection = factory.CreateConnection();

                return true;
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
                return false;
            }
        }

        public void Disconnect()
        {
            Connection.Close();
        }
    }
}
